﻿package com.bizmlib.utils
{
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.text.TextField;
	import flash.text.TextFormat;
	
	import com.bizmlib.lang.Locale;
	import com.bizmlib.lang.LocaleEvent;
	//import com.bizmlib.utils.array.ArrayHandling;
	
	
	public class TextController extends EventDispatcher
	{
		private static const STATE_NONE:int = 0;
		private static const STATE_RUN:int = 1;
		private static const STATE_CLEAN:int = 2;
		
		private var __adapter:TextControllerAdapter;
		private var __ids:*;
		private var stateRun:int;
		private var text:String;
		private var __textField;TextField;
		private var textFormat:TextFormat;
		
		public var keepTextFormat:Boolean;
		public var html:Boolean;
		
		
		public function TextController()
		{
			init();
		}
		
		
		public function get ids():String
		{
			return __ids;
		}
		public function set ids(value:String):void
		{
			//trace(this + ".ids (set) " + arguments);
			
			if(stateRun==STATE_CLEAN)
			{
				value = null;
			}
			
			if(ids==value)
			{
				return;
			}
			
			__ids = value;
			
			updateText();
		}
		
		public function set textField(value:TextField):void
		{
			//trace(this + ".textField (set) " + arguments);
			
			if(stateRun==STATE_CLEAN)
			{
				value = null;
			}
			if(textField==value)
			{
				return;
			}
			
			__textField = value;
			textFormat = textField ? textField.getTextFormat() : null;
			//trace("textFormat\t" + textFormat.font + "\t" + textFormat.bold + "\t" + textFormat.italic + "\t" + textFormat.kerning + "\t" + textFormat.leading + "\t" + textFormat.size);
			
			updateText();
		}
		public function get textField():TextField
		{
			return __textField;
		}
		
		public function set adapter(value:TextControllerAdapter):void
		{
			if(stateRun==STATE_CLEAN)
			{
				value = null;
			}
			if(value==adapter)
			{
				return;
			}
			__adapter = value;
			updateText();
		}
		public function get adapter():TextControllerAdapter
		{
			return __adapter;
		}
		
		
		public function clean():void
		{
			doClean();
		}
		
		
		protected function init():void
		{
			stateRun = STATE_RUN;
			
			text = "";
			
			Locale.getInstance().addEventListener(LocaleEvent.CHANGE, localeChange);
		}
		protected function doClean():void
		{
			stateRun = STATE_CLEAN;
			
			Locale.getInstance().removeEventListener(LocaleEvent.CHANGE, localeChange);
			
			textField = null;
			textFormat = null;
			adapter = null;
		}
		
		protected function localeChange(event:LocaleEvent):void
		{
			//trace(this + ".localeChange(" + arguments + ")");
			
			//if(!(dataProvider && event.idsList && (event.idsList is Array)))
			//{
			//	return;
			//}
			//var idsList:Array = event.idsList;
			//if(!ArrayHandling.isItemInArray(idsList, dataProvider.ids))
			//{
			//	return;
			//}
			
			updateText();
		}
		
		protected function updateText():void
		{
			//trace(this + ".updateText(" + arguments + ")");
			
			if(stateRun==STATE_CLEAN)
			{
				return;
			}
			
			var text:String = Locale.loadString(ids);
			setTextFieldText(adapter ? String(adapter.adaptMethod(text)) : text);
		}
		
		private function setTextFieldText(text:String):void
		{
			//trace(this + ".setTextFieldText(" + arguments + ")");
			
			if(!textField || text==this.text)
			{
				return;
			}
			
			this.text = text || "";
			if(html)
			{
				textField.htmlText = this.text;
			}
			else
			{
				textField.text = this.text;
			}
			if(textFormat && keepTextFormat)
			{
				textField.setTextFormat(textFormat);
			}
			
			dispatchEvent(new Event(Event.CHANGE, false, true));
		}
		
		
		override public function toString():String
		{
			return "[TextEditController	stateRun:" + stateRun + "	textField:" + textField + "	ids:" + ids + "]";
		}
	}
}